#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QFileDialog>
#include "opencv.h"
#include <QFrame>
#include "Imshow_frame.h"
#include <QTimer>
#include <QDialog>
#include "algorithm_choose.h"
//#include <QApplication>
//#include <QDesktopWidget>
Mat image_left;
bool mod_global;

MainWindow::MainWindow(int width,int height,bool mod,QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    mod_global = mod;
    if (mod)
        Mainwindow_init_i(width,height);
    else
        Mainwindow_init_ii(width,height);
    timer = new QTimer(this);
    //timer2 = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(display_fun()));
    //connect(timer2,SIGNAL(timeout(),this,SLOT(display_setting()));
    timer->start(100);
    //timer2->start(100);
}

MainWindow::~MainWindow()
{
    delete ui;
    delete m_pstatus;
    delete img_left;
    delete img_right;
    delete img_only;
    delete timer;
}

void MainWindow::on_leftload_clicked()
{

    QString fileName = QFileDialog::getOpenFileName(this,tr("Open Image"),".",tr("Image Files (*.png *.jpg *.bmp);;All Files (*.*)"));
    image_left = imread(fileName.toLocal8Bit().toStdString());
    img_left->frame_filename = fileName;
    img_left->imgshow_flag = true;
}

void MainWindow::display_fun()
{
    if(img_left->imgshow_flag)
    {
        if(img_right->imgshow_flag)
            m_pstatus->showMessage(QString("左图像坐标:x:%1,y:%2   右图像坐标：x:%3,y:%4").arg(img_left->mouse_x).arg(img_left->mouse_y).arg(img_right->mouse_x).arg(img_right->mouse_y));
        else
            m_pstatus->showMessage(QString("左图像坐标:x:%1,y:%2").arg(img_left->mouse_x).arg(img_left->mouse_y));
    }
    else
    {
        if (img_right->imgshow_flag)
        m_pstatus->showMessage(QString("右图像坐标:x:%1,y:%2").arg(img_right->mouse_x).arg(img_right->mouse_y));
    }
    if(img_only->imgshow_flag)
        m_pstatus->showMessage(QString("图像坐标:x:%1,y:%2").arg(img_only->mouse_x).arg(img_only->mouse_y));
    if((img_left->imgshow_flag && img_right->imgshow_flag)||img_only->imgshow_flag )
        this->ui->Go_button->setEnabled(true);
    else
        this->ui->Go_button->setEnabled(false);
}

void MainWindow::Mainwindow_init_ii(int width,int height)
{
    ui->setupUi(this);
    this->setGeometry(100,100,2*width+50,1.5*height);
    this->setMaximumSize(2*width+20,1.5*height);
    this->setMinimumSize(2*width+20,1.5*height);
    ui->Go_button->setGeometry(this->width()-150,1.2*height+16,75,23);
    ui->Go_button->setEnabled(false);
    ui->onlyload->setVisible(false);
    ui->onlyclear->setVisible(false);
    ui->leftload->setGeometry(50,1.2*height,75,23);
    ui->rightload->setGeometry(50,1.2*height+33,75,23);
    ui->leftclear->setGeometry(50+80,1.2*height,75,23);
    ui->rightclear->setGeometry(50+80,1.2*height+33,75,23);
    ui->pushButton->setGeometry(50+160,1.2*height+16,75,23);
    img_left = new Imshow_frame(this,10,30,width,height);
    img_right = new Imshow_frame(this,this->geometry().width()-width-10,30,width,height);
    img_only = new Imshow_frame(this,10,30,width,height);
    img_only->setVisible(false);
    m_pstatus = new QStatusBar(this);
    setStatusBar(m_pstatus);
}

void MainWindow::Mainwindow_init_i(int width,int height)
{
    ui->setupUi(this);
    this->setGeometry(100,100,2*width+50,1.5*height);
    this->setMaximumSize(2*width+20,1.5*height);
    this->setMinimumSize(2*width+20,1.5*height);
    ui->Go_button->setGeometry(this->width()-150,1.2*height+30,75,40);
    ui->Go_button->setEnabled(false);
    ui->onlyload->setGeometry(50,1.2*height,75,40);
    ui->onlyclear->setGeometry(50,1.2*height+60,75,40);
    ui->leftload->setVisible(false);
    ui->rightload->setVisible(false);
    ui->leftclear->setVisible(false);
    ui->rightclear->setVisible(false);
    ui->pushButton->setGeometry(50+160,1.2*height+30,75,40);
    img_left = new Imshow_frame(this,10,30,width,height);
    img_right = new Imshow_frame(this,this->geometry().width()-width-10,30,width,height);
    img_left->setVisible(false);
    img_right->setVisible(false);
    img_only = new Imshow_frame(this,10,30,width,height);
    img_only->move((this->width()-img_only->width())/2,(this->height()-img_only->height())/2.5);
    m_pstatus = new QStatusBar(this);
    setStatusBar(m_pstatus);
}

void MainWindow::on_pushButton_clicked()
{
    Setting* setting_new = new Setting(false);
    setting_new->show();
    setting_new->move(this->x()+(this->width()-setting_new->width())/2,this->y()+(this->height()/2-setting_new->height()/2));
    this->setEnabled(false);
    if (setting_new->exec() == QDialog::Accepted)
    {
        //delete img_left;
        //delete img_right;
        this->close();
        MainWindow* new_window = new MainWindow(setting_new->img_width,setting_new->img_height,setting_new->trans_flag);
        delete setting_new;
        new_window->show();
    }
   else
    {
        setting_new->close();
        this->setEnabled(true);
        delete setting_new;
    }

}

void MainWindow::on_rightload_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,tr("Open Image"),".",tr("Image Files (*.png *.jpg *.bmp);;All Files (*.*)"));
    img_right->frame_filename = fileName;
    img_right->imgshow_flag = true;
}

void MainWindow::on_leftclear_clicked()
{
    img_left->imgshow_flag = false;
}

void MainWindow::on_rightclear_clicked()
{
    img_right->imgshow_flag = false;
}

void MainWindow::on_onlyload_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,tr("Open Image"),".",tr("Image Files (*.png *.jpg *.bmp);;All Files (*.*)"));
    img_only->frame_filename = fileName;
    img_only->imgshow_flag = true;
}

void MainWindow::on_onlyclear_clicked()
{
    img_only->imgshow_flag = false;
}

void MainWindow::on_Go_button_clicked()
{

    Algorithm_choose* choose = new Algorithm_choose(mod_global);
    choose->move(this->x()+(this->width()-choose->width())/2,this->y()+(this->height()/2-choose->height()/2));

    choose->show();
    this->setEnabled(false);
    while(~choose->exec()== QDialog::Accepted)
    choose->close();
    this->setEnabled(true);
    delete choose;
    switch(choose->algorithm)
    {
        case 1:
                ;
        default:
            break;
    }

}
